﻿using System;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.Extensions.Logging;
using WMS.Api.Application.Commands;
using WMS.Common;
using WMS.Domain;
using WMS.Infrastructure;

namespace WMS.Api.Application.CommandHandlers
{
    /// <summary>
    /// 库存出入库添加处理程序
    /// </summary>
    public class CreateInventoryModelHander : IRequestHandler<CreateInventoryModelCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<InventoryModel> _inventoryModelRepository;
   

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="inventoryModelRepository">库存仓储接口</param>

        public CreateInventoryModelHander(IBaseRepository<InventoryModel> inventoryModelRepository)
        {
            _inventoryModelRepository = inventoryModelRepository ;
           
        }

        /// <summary>
        /// 处理库存出入库添加命令
        /// </summary>
        /// <param name="request">库存出入库添加命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>添加是否成功</returns>
        public async Task<ApiResult<int>> Handle(CreateInventoryModelCommand request, CancellationToken cancellationToken)
        {
            // 创建新的库存出入库记录
            var inventory = new InventoryModel
            {
                InventoryTeam = request.InventoryTeam,
                InventoryType = request.InventoryType,
                Speak = request.Speak,
                InventoryNum = request.InventoryNum,
                BoxNumber = request.BoxNumber,
                ProductsSku = request.ProductsSku
            };

            // 添加到数据库
            var o = await _inventoryModelRepository.AddNewAsync(inventory);
            
            return new ApiResult<int>("添加库存出入库记录成功") { data = o, isSuc = true };
        }
    }
}
